package com.metroinfo.workers.selector;

import java.io.*;
import java.nio.*;
import java.nio.charset.*;

import com.metroinfo.workers.ChannelIO;

/**
 * An object used for sending Content to the requestor.
 * 
 * @author Mark Reinhold
 * @author Brad R. Wetmore
 * @version 1.4, 10/01/12
 */
class Reply implements Sendable {

	private Content content;
	private boolean headersOnly;


	public Reply(Content c) {
		content = c;
	}

	private ByteBuffer hbb = null;

/*	private ByteBuffer headers() {
		return null;
	}
*/
	public void prepare() throws IOException {
		content.prepare();
//		hbb = headers();
	}

	public boolean send(ChannelIO cio) throws IOException {

		if (hbb == null)
			throw new IllegalStateException();

		if (hbb.hasRemaining()) {
			if (cio.write(hbb) <= 0)
				return true;
		}

		if (!headersOnly) {
			if (content.send(cio))
				return true;
		}

		if (!cio.dataFlush())
			return true;

		return false;
	}

	public void release() throws IOException {
		content.release();
	}
}
